package com.inossem.warehousestorage_inossem.activity.allocation.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.out_storage.extra.AllocationOutStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStoragePickingOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageResultActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationOutMaterialsHaveSearchAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutOverViewBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutSubmitBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationOutSubmitRequestBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutStorageSearchMaterialsRequest;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationOutSubmitRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 物料列表 帶搜索
 *
 * @author 詹建宇
 * @time on 2018/3/30 10:23
 * @email jianyu.zhan@inossem.com
 */
public class AllocationOutMaterialsHaveSearchActivity extends BaseActivity implements BaseInteface {

    private TextView title, nextStep;
    private PullableListView listView;

    private List<ItemListBean> allData;//物料列表

    private String location_id_output;//出库地点ID
    private String location_id_input;//入库地点ID
    private String location_name_input;//入库地点Name
    private String fty_id;//工厂ID

    private String mCondition;//关键字

    private AllocationOutMaterialsHaveSearchAdapter adapter;
    private String stock_output_id;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_allocating_materials);

        initView(this);
        SPUtils.removeAllotionOutStorageInformation(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        initData(this);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        listView = (PullableListView) activity.findViewById(R.id.listView);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);

        listView.setPullDownFlag(false);
        listView.setPullUpFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText("物料列表");
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_in_storage_receive_list_title_short), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(activity, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (adapter.isOk()) {
                    submitSN(activity);
                } else {
                    Utils.middleToast(activity, getResources().getString(R.string.activity_in_storage_free_overview_at_least_one_item));
                }
            }
        });
        allData = new ArrayList<>();
        mCondition = "";
        getExtra(activity);
        getData(activity);
    }

    private void getExtra(Activity activity) {
        location_id_output = activity.getIntent().getStringExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_ID_OUTPUT);
        location_id_input = activity.getIntent().getStringExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_ID_INPUT);
        location_name_input = activity.getIntent().getStringExtra(AllocationOutStorageExtra.ALLOCATION_LOCATION_NAME_INPUT);
        fty_id = activity.getIntent().getStringExtra(AllocationOutStorageExtra.FTY_ID);
    }

    private void getData(Context context) {
        String pickingOutStorageInformation = SPUtils.getAllotionOutStorageInformation(context);
        if (TextUtils.isEmpty(pickingOutStorageInformation)) {
            addListData(context, allData, listView, true);
        } else {
            Gson gson = new Gson();
            AllocationOutOverViewBean bean = gson.fromJson(pickingOutStorageInformation, AllocationOutOverViewBean.class);
            allData = bean.getBody().getItem_list();
            title.setText(bean.getBody().getRefer_receipt_code());
            addListData(context, ActivityUtils.getShowData(allData, 0), listView, true);
        }
    }

    private void addListData(Context context, List<ItemListBean> list, ListView listView, boolean flag) {
        adapter = new AllocationOutMaterialsHaveSearchAdapter(context, list, mCondition, location_id_output, new OutSearchListener() {
            @Override
            public NoDoubleClickListener getNoDoubleClickListener(EditText condition) {
                NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
                    @Override
                    public void onNoDoubleClick(View v) {
                        Utils.closeSoftKeyboard(context, v);
                        mCondition = condition.getText().toString();
                        if (TextUtils.isEmpty(mCondition)) {
                            return;
                        }
                        search(context, mCondition, location_id_output, null, null);
                    }
                };
                return noDoubleClickListener;
            }
        });
        listView.setAdapter(adapter);
    }

    private void search(Context context, String condition, String location_id, String batch, String stock_input_id) {
        AllocationOutStorageSearchMaterialsRequest.request(context, condition, location_id, batch, stock_input_id, new GsonBeanListener<AllocationOutOverViewBean>() {
            @Override
            public void onSuccess(AllocationOutOverViewBean bean) {
                List<ItemListBean> beanList = bean.getBody().getItem_list();

                //判断新添加的物料 是否跟第一个项目相同
                boolean isOk = true;
                for (ItemListBean listBean : beanList) {
                    if (!TextUtils.isEmpty(adapter.getMat_store_type_first()) && !listBean.getMat_store_type().equals(adapter.getMat_store_type_first())) {
                        isOk = false;
                        break;
                    }
                }

                if (!isOk) {
                    Utils.middleToast(context, "新添加的物料与当前物料类别不相同,清重新添加");
                    return;
                }

                allData = AllocationUtils.filterItemList(allData, beanList);

                for (ItemListBean listBean : allData) {
                    listBean.setLocation_name(location_name_input);
                    listBean.setLocation_id(location_id_input);
                    listBean.setFty_id(fty_id);
                }

                Gson gson = new Gson();
                bean.getBody().setItem_list(allData);
                String json = gson.toJson(bean, AllocationOutOverViewBean.class);
                //保存数据
                SPUtils.saveAllocationOutStorageInformation(context, json);
                //显示数据到ListView
                addListData(context, allData, listView, true);
            }
        });
    }

    public interface OutSearchListener {
        NoDoubleClickListener getNoDoubleClickListener(EditText condition);
    }

    private void submitSN(Activity activity) {

        AllocationOutSubmitRequestBean submitRequestBean = AllocationUtils.getAllocationOutSubmitRequestBean(activity, stock_output_id, "");

        AllocationOutSubmitRequest.request(activity, submitRequestBean, TextUtils.isEmpty(stock_output_id) ? "0" : "", new GsonBeanListener<AllocationOutSubmitBean>() {
            @Override
            public void onSuccess(AllocationOutSubmitBean bean) {
                LogUtils.i("onSuccess:" + bean.getBody());
                SPUtils.removePickingOutStorageInformation(activity);
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(activity, bean.getHead().getMsg());
                }
                Intent intent = new Intent(activity, OutStorageResultActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFailing(AllocationOutSubmitBean bean) {
                super.onFailing(bean);
                stock_output_id = bean.getBody();
            }
        });
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(this, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }
}
